package com.qf.microuser.user.controller;

import com.qf.common.core.annotation.NoLogin;
import com.qf.common.core.common.R;
import com.qf.common.core.constants.AliyunOSSConstants;
import com.qf.common.core.entity.User;
import com.qf.common.core.util.AliyunOSSUtil;
import com.qf.common.core.util.UserUtils;
import com.qf.common.neo4j.node.UserNode;
import com.qf.microuser.user.service.UserService;
import com.qf.microuser.user.vo.UserInfoVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 天--道--酬--勤
 *
 * @author LiangGaoQiang
 * @ClassName UserController
 * @Date: 2025/5/22 14:03
 * @Description: 描述
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 获取用户信息
     * @return 用户信息
     */
    @GetMapping("/my")
    public R userInfo(){
        // 从ThreadLocal对象中获取uid
        Integer uid = UserUtils.getUid();
        if (uid == null) {
            return R.fail();
        }
        // 调用userService.getUserInfo(uid)方法获取用户信息
        UserInfoVO userInfoVO = userService.getUserInfo(uid);
        if (userInfoVO == null) {
            return R.fail();
        }
        return R.ok(userInfoVO);
    }

    /**
     * 获取用户详情
     * @return 用户详情
     */
    @GetMapping("/info")
    public R userDetails(){
        Integer uid = UserUtils.getUid();
        if (uid == null) {
            return R.fail();
        }
        UserInfoVO userInfoVO = userService.getUserInfo(uid);
        if (userInfoVO == null) {
            return R.fail();
        }
        return R.ok(userInfoVO);
    }


    /**
     * 上传头像
     * @param file 头像文件
     * @return 上传结果
     */
    @PostMapping("/uploadAvatar")
    public R upload(MultipartFile file){
        String filePath = AliyunOSSUtil.upLoad(file);
        // 更新用户头像的url，将OSS文件路径的最后一段存储到数据库中
        User user = new User();
        Integer uid = UserUtils.getUid();
        if (uid == null) {
            return R.fail();
        }
        user.setId(uid);
        user.setAvatar(filePath.substring(AliyunOSSConstants.PUBLIC_URL.length()));
        boolean b = userService.updateUserInfoById(user);
        if (!b){
            return R.fail();
        }
        return R.ok(filePath);
    }


    /**
     * 修改用户信息
     * @param user 用户信息
     * @return 修改结果
     */
    @PostMapping("/update")
    public R updateUserInfo(@RequestBody User user){
        Integer uid = UserUtils.getUid();
        if (uid == null) {
            return R.fail();
        }
        user.setId(uid);
        boolean result = userService.updateUserInfoById(user);
        if (!result) {
            return R.fail();
        }
        return R.ok();
    }
    /**
     * 获取所有父级用户
     * @param userId 当前用户id
     * @return 所有父级用户
     */
    @GetMapping("/findAllParentUsers")
    public R findAllParentUsers(Integer userId){
        List<User> parentUsers = userService.findAllParentUsers(userId);
        if (parentUsers.isEmpty()) {
            return R.fail();
        }
        System.out.println("parentUsers = " + parentUsers);
        return R.ok(parentUsers);
    }

    @GetMapping("/invite")
    public R invite(Integer userId){
        UserNode userNode = userService.getUserNodeinvite(userId);
        return R.ok(userNode);
    }

    @PostMapping("/uploadAvatar2")
    @NoLogin
    public R uploadAvatar(MultipartFile file){
      String path = userService.uploadAvatar(file);
      return R.ok(path);
    }
}
